package com.ruoyi.framework.mq.RabbitMQ;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@EnableRabbit
@Configuration
public class ConsumMQ {
    private static final Logger log=LoggerFactory.getLogger(ConsumMQ.class);

    /**
     * 简单模式和work模式的消费者1
     * queues 监听的队列名称
     * @param
     */
    @RabbitListener(queues = RabbitMqConstants.DIRECT_QUEUE)
    @RabbitHandler
    public void consum1(Message message,Channel channel)throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("消费者1消费消息:"+new String(message.getBody()).toString());
    }

    /**
     * 简单模式和work模式的消费者2
     * @param
     */
    @RabbitListener(queues = RabbitMqConstants.DIRECT_QUEUE)
    @RabbitHandler
    public void consum2(Message message,Channel channel) throws IOException{
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("消费者2消费消息:"+new String(message.getBody()).toString());
    }

    @RabbitListener(queues = RabbitMqConstants.FANOUT_QUEUE_1)
    @RabbitHandler
    public void consumFanout1A(Message message,Channel channel)throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("Fanout1A消费消息:"+new String(message.getBody()).toString());
    }

    @RabbitListener(queues = RabbitMqConstants.FANOUT_QUEUE_2)
    @RabbitHandler
    public void consumFanout2A(Message message,Channel channel) throws IOException{
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("Fanout2A消费消息:"+new String(message.getBody()).toString());
    }

//    @RabbitListener(queues = RabbitMqConstants.FANOUT_QUEUE_1)
//    @RabbitHandler
//    public void consumFanout1B(String msg) {
//        log.info("Fanout1B消费消息:"+msg);
//    }
//
//    @RabbitListener(queues = RabbitMqConstants.FANOUT_QUEUE_2)
//    @RabbitHandler
//    public void consumFanout2B(String msg) {
//        log.info("Fanout2B消费消息:"+msg);
//    }


    @RabbitListener(queues = RabbitMqConstants.TOPIC_QUEUE_1)
    @RabbitHandler
    public void consumTopic1(Message message,Channel channel)throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("topic1消费消息:"+new String(message.getBody()).toString());
    }

    @RabbitListener(queues = RabbitMqConstants.TOPIC_QUEUE_2)
    @RabbitHandler
    public void consumTopic2(String msg) {
        log.info("topic2消费消息:"+msg);
    }

    @RabbitListener(queues = RabbitMqConfig.QueueName.ImmediateQueue)
    @RabbitHandler
    public void consumDelaye(Message message,Channel channel)throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("延迟队列ttl延迟队列消费消息:"+new String(message.getBody()).toString());
    }

    @RabbitListener(queues = RabbitMqConfig.QueueName.XImmediateQueue)
    @RabbitHandler
    public void consumXDelaye(Message message,Channel channel) throws IOException {
        // 采用手动应答模式, 手动确认应答更为安全稳定
           channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.info("延迟队列插件消费消息:"+new String(message.getBody()).toString());
    }
}
